<?php
/**
 * 这里是十一选五返奖
 */
defined('IN_YuLin') || exit('NO PERMIT!');
echo '<pre>';
// 获取十一选五所有的彩种信息
$lotterySql  = 'SELECT lott.id, lott.`sname`, lott.`title`, lott.`ptid` FROM ' . Table('lottery') . ' AS lott
                LEFT JOIN ' . Table('lottery_category') . ' AS cate
                ON lott.`cate_id` = cate.`id`
                WHERE cate.`sname` = "ssc"';
$lotteryList = $db->getall($lotterySql);
// 获取到所有的十一选五彩种，根据彩种设置sname信息来获取开奖接口内容
$qihaoTab    = Table('lottery_k3');
$itemTab     = Table('lottery_order_item');
$mainTab     = Table('lottery_order_main');
foreach ($lotteryList as $k => $v){
// 获取当前彩种最新的开奖期号
    $qihaoSql  = 'SELECT id, period, tid, `code` FROM ' . $qihaoTab . ' WHERE tid = ' . $v['id'] . ' AND `code` != "" ORDER BY id DESC LIMIT 10';
    $qihaoList = $db->getall($qihaoSql);
    $qhStr     = ' (0';
    foreach ($qihaoList as $qk => $qv){
        $qhStr .= ',' . $qv['id'];
    }
    $qhStr     .= ')';
    $qihaoStr  = ' item.`period_id` IN ' . $qhStr . ' AND item.prize_status = 1 AND item.`winner` = 2 ';
//     $getAllWinnerItemSql = 'SELECT main.`uid`, main.order_no, item.id, item.period_id, item.lott_id, item.cn_play, item.betting_no, item.`winner`, item.o_id, item.`money`, item.`money_unit`, item.`num`, item.`odds`, item.`multiple`, item.`eo_money`, item.`winner_end`, item.`status` FROM 
//             ' . Table('lottery_order_item') . ' AS item
//             LEFT JOIN ' . Table('lottery_order_main') . ' AS main
//             ON item.`o_id` = main.`id`
//             WHERE ' . $qihaoStr . ' AND item.`winner` = 2 ORDER BY item.period_id asc';
    $getAllWinnerItemSql = 'SELECT
            main.`uid`,
            item.id, item.o_id, item.`lott_id`, item.`betting_no`, item.`betting_str`, item.`chase`, item.`winner_end`,
                item.`eo_money`, item.`money_unit`, item.`multiple`, item.`num`, item.`odds`
            FROM ' . $itemTab . ' AS item
            LEFT JOIN ' . $mainTab . ' AS main
            ON item.`o_id` = main.`id`
            WHERE ' . $qihaoStr . ' ORDER BY item.period_id asc';
    $winnerList  = $db->getall($getAllWinnerItemSql);
    if (empty($winnerList)) continue;
    $orderWinner = array();
    foreach ($winnerList as $k1 => $v1){
        // 整理投注信息， 按订单用户分类
        $orderWinner[$v1['o_id']][] = $v1;
    }
}
// 如果是中奖后停止追单，这里记录订单号，
$winnerEndOrderArr = array();
if (!$orderWinner) {
    echo 'NO Data';
    return;
}
foreach ($orderWinner as $ok => $ov){
    $orderPrize = 0;
    foreach ($ov as $key => $value){
        // 未中奖跳过
        if($value['winner'] == 1) continue;
        $winnerEndOrderArr[$value['oid']] = $value['period_id'];
        // 计算中奖金额，准备返奖[投注数量 * 赔率 * 投注金额单位 * 倍数 * 每注金额 ]
        $winnerMoney = $value['num'] * $value['odds'] * $value['money_unit'] * $value['multiple'] * $value['eo_money'];
        $tmp = [];
        $tmp['uid']     =   $value['uid'];
        $tmp['tid']     =   'betting_award';
        $tmp['lkey']    =   'FZ' . $value['order_no'];
        $tmp['money']   =   $winnerMoney;
        $tmp['balance'] =   -$winnerMoney;
        $tmp['notes']   =   '彩票中奖：【' . $value['cn_play'] . ':' . $value['betting_no'] . '】';
        MoneyLog($tmp);
        // 修改投注状态与返奖状态
        $sql = 'UPDATE ' . Table('lottery_order_item') . ' SET prize_status = 2, prize = ' . $winnerMoney . ' WHERE id = ' . $value['id'];
        $db->exec($sql);
        $orderPrize += $winnerMoney;
    }
    // 更新订单中奖信息
    $sql = 'UPDATE ' . Table('lottery_order_main') . ' SET prize = ' . $orderPrize . ' WHERE id = ' . $ok;
    $db->exec($sql);
}
echo 'END';


